package cn.gov.pbc.gd.credit.web.controller.login;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.liu.jbase.BaseVo;
import com.liu.jbase.ResultVo;
import com.liu.jbase.constant.ResCode;
import com.liu.jbase.controller.impl.BaseControllerImpl;
import com.liu.tools.NetHelper;
import com.liu.tools.WebHelper;

import cn.gov.pbc.gd.credit.common.constant.CodeConstant;
import cn.gov.pbc.gd.credit.common.model.UserModel;
import cn.gov.pbc.gd.credit.common.vo.LoginVo;
import cn.gov.pbc.gd.credit.user.service.login.ILoginService;
import cn.gov.pbc.gd.credit.user.service.user.IUserService;
import cn.gov.pbc.gd.credit.vo.user.ResUserVo;
import cn.gov.pbc.gd.credit.vo.user.UserVo;

@Controller
@RequestMapping("")	
public class LoginController extends BaseControllerImpl<ResUserVo, UserVo, IUserService> {
	
	@Autowired
	private ILoginService loginService;
	
	@ResponseBody
	@RequestMapping("/user/login")
	public ResultVo<UserModel> login(LoginVo vo, Model model, HttpServletRequest request, HttpServletResponse response,HttpSession httpSession){
		
		ResultVo<UserModel> resultVo = loginService.login(vo, request);
		String sid = "";
        if(ResCode.SUCCESS.equals(resultVo.getCode())){
			
			sid = resultVo.getData().getSid();
			httpSession.setAttribute("account", resultVo.getData().getAccount());
			WebHelper.setCookie(response, sid, resultVo.getData().getCookiesKey(), "/", request.getServerName(), -1, true);
			
		}
		
		return resultVo;
	}
	
	@RequestMapping("/user/logout")
	public String logout(BaseVo vo, Model model, HttpServletRequest request, HttpServletResponse response,HttpSession httpSession){
		
		ResultVo<UserModel> resultVo = loginService.logout(vo, request);
		String sid = NetHelper.getReqParams(request, "sid", "");
        if(ResCode.SUCCESS.equals(resultVo.getCode())){
        	httpSession.setAttribute("account","");
        	WebHelper.clearCookie(request, response, sid,  "/", request.getServerName());			
		}
		
		return "redirect:/login";
	}
	
	@ResponseBody
	@RequestMapping("/user/offline")
	public ResultVo<UserModel> offline(BaseVo vo, HttpServletRequest request){
		
		return loginService.logout(vo, request);
	}
	
	@ResponseBody
	@RequestMapping("/user/face/img")
    public ResultVo<String> userFaceCertImg(LoginVo vo, HttpServletRequest request, HttpServletResponse response) throws Exception {  
    	
		return loginService.findFaceCertifiedImg(vo, request, response);
    } 
	
	@ResponseBody
	@RequestMapping("/login/conf")
    public ResultVo<Map<String, Object>> loginConf(LoginVo vo, HttpServletRequest request) throws Exception {  
    	
		return loginService.loginConf(vo, request);
    } 
	
	@ResponseBody
	@RequestMapping("/check/sid")
    public ResultVo<String> checkSid(LoginVo vo, HttpServletRequest request) throws Exception {  
		
		ResultVo<String> resultVo = new ResultVo<>();
		resultVo.setCode(CodeConstant.SUCCESS);
		return resultVo;

    } 
}
